package com.eugenes.util.date;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Period;

public class TimePeriodValidator implements DateTimeValidator {

    private DateTime start;
    private DateTime finish;

    public TimePeriodValidator(DateTime start, DateTime finish) {
        if (start.isAfter(finish)) {
            throw new IllegalArgumentException("Start > finish.");
        }

        // Note - DateTime class is immutable
        // so no defensive copy
        this.start = start;
        this.finish = finish;
    }

    public TimePeriodValidator(DateTime start, Period period) {
        this(start, start.plus(period));
    }

    public TimePeriodValidator(DateTime start, Duration duration) {
        this(start, start.plus(duration));
    }

    @Override
    public boolean isValid(DateTime dateTime) {
        return dateTime.isBefore(finish)
                && (dateTime.isAfter(start) || dateTime.isEqual(start));
    }
}
